import {Notify} from "../../models/notify";
Page({
    data: {
        notifyList: [],
        page: 1,
        hasMore: true
    },

    async onLoad (options) {
        await this.getNotifyList()
    },

    async getNotifyList() {
        const res = await Notify.getNotifyList(this.data.page, this.data.notifyList.length)
        const notifyList = this.data.notifyList.concat((res.list))
        this.data.hasMore = res.hasMore

        notifyList.forEach(item => item.timestamp = new Date(item.create_time).getTime())
        const newestNotifyIndex = notifyList.findIndex(item => item.timestamp === Math.max(...(notifyList.map(item => item.timestamp))))
        this.setData({
            notifyList,
            [`notifyList[${newestNotifyIndex}].newest`]: true
        })
    },

    async onReachBottom() {
        if(!this.data.hasMore) return
        this.data.page++
        await this.getNotifyList()
    }
});
